package com.gocoolmall.order.controller;

import com.gocoolmall.order.entity.OrderEntity;
import com.gocoolmall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

@RestController
public class RabbitController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMaq")
    public String sendMq(@RequestParam(value = "num",defaultValue = "10") Integer num){
        for (int i = 0; i < num; i++) {
            if (i%2 == 0){
                OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
                reasonEntity.setId(1L);
                reasonEntity.setCreateTime(new Date());
                reasonEntity.setName("哈啊"+i);
                //对象被默认序列化以后发送出去
                rabbitTemplate.convertAndSend("hello-java-exchange","hello.java",reasonEntity,
                        new CorrelationData(UUID.randomUUID().toString()));
            }else {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setOrderSn(UUID.randomUUID().toString());
                orderEntity.setBillType(i);
                rabbitTemplate.convertAndSend("hello-java-exchange","hello22.java", orderEntity,
                        new CorrelationData(UUID.randomUUID().toString()));
            }
        }
        System.out.println("OK");
        return "消息发送完成ok";
    }
}
